#!/bin/bash

NODETYPE=$1

if [ "${NODETYPE}" == "master" ]; then
    TARS=(tarsAdminRegistry tarslog tarspatch tarsconfig tarsnode tarsnotify tarsproperty tarsqueryproperty tarsquerystat tarsregistry tarsstat)
else
    TARS=(tarsnode)
fi

#killall all tars-servers
for var in ${TARS[@]};
do
  killall -9 -q ${var}
done

#获取主机hostip
HOSTIP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 0.0.0.0|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

if [ "$HOSTIP" == "127.0.0.1" ] || [ "$HOSTIP" == "" ]; then
    echo "HOSTIP:[$HOSTIP], not valid. HOSTIP must not be 127.0.0.1 or empty."
    exit -1
fi

#check port
if [ "${NODETYPE}" == "master" ]; then
  PORTS=(18993 18793 18693 18193 18593 18493 18393 18293 12000 19385 17890 17891)
  for P in ${PORTS[@]};
  do
      RESULT=`netstat -lpn | grep ${HOSTIP}:${P} | grep tcp`
      if [ "$RESULT" != "" ]; then
          LOG_ERROR ${HOSTIP}:${P}" confict!!"
          exit -1
      fi
      RESULT=`netstat -lpn | grep 127.0.0.1:${P} | grep tcp`
      if [ "$RESULT" != "" ]; then
          LOG_ERROR 127.0.0.1:${P}" confict!!"
          exit -1
      fi
  done
fi

#check mysql
if [ "${NODETYPE}" == "master" ]; then
  while [ 1 ]
  do
      RESULT=`mysqladmin -h${DBIP} -u${DBUser} -p${DBPassword} -P${DBPort} ping`

      echo $RESULT | grep -q "alive"
      if [ $? == 0 ]; then
          LOG_INFO "mysql is alive"
          break
      fi

      echo "check mysql is not alive: mysqladmin -h${DBIP} -u${DBUser} -p${DBPassword} -P${DBPort} ping"

      sleep 3
  done
fi

function update_conf() {
    if [ "$NODETYPE" == "master" ]; then
        sed -i "s/localip.tars.com/$HOSTIP/g" `grep localip.tars.com -rl ${TARS_INSTALL}/$1/conf/tars.$1.config.conf`
        sed -i "s/db.tars.com/${DBIP}/g" `grep db.tars.com -rl ${TARS_INSTALL}/$1/conf/tars.$1.config.conf`
        sed -i "s/registry.tars.com/$HOSTIP/g" `grep registry.tars.com -rl ${TARS_INSTALL}/$1/conf/tars.$1.config.conf`
    else
        sed -i "s/localip.tars.com/$HOSTIP/g" `grep localip.tars.com -rl ${TARS_INSTALL}/$1/conf/tars.$1.config.conf`
        sed -i "s/registryAddress/tcp -h $MASTER -p 17890/g" `grep registryAddress -rl ${TARS_INSTALL}/$1/conf/tars.$1.config.conf`
        sed -i "s/registryAddress/tcp -h $MASTER -p 17890/g" `grep registryAddress -rl ${TARS_INSTALL}/$1/util/execute.sh`
    fi
}

#update server config
for var in ${TARS[@]};
do
    update_conf ${var}
done

install_master_DB(){
	# modify core service config
	echo "modify core service config"
	cd ${TARS_INSTALL}
  if [ "$NODETYPE" == "master" ]; then
    sed -i "s/198.1.3.254/${HOSTIP}/g" `grep 198.1.3.254  -rl ./*`
    sed -i "s/192.168.2.131/${HOSTIP}/g" `grep 92.168.2.131  -rl ./*`
    sed -i "s/registry.tars.com/${HOSTIP}/g" `grep registry.tars.com -rl ./*`

    sed -i "s/db.tars.com/${DBIP}/g" `grep db.tars.com -rl ./*`
    sed -i "s/dbport=3306/dbport=${DBPort}/g" `grep dbport=3306 -rl ./*`
    sed -i "s/dbuser=tars/dbuser=${DBUser}/g" `grep dbuser=tars -rl ./*`
    sed -i "s/dbpass=tars2015/dbpass=${DBPassword}/g" `grep dbpass=tars2015 -rl ./*`

  else
    sed -i "s/registry.tars.com/${MASTER}/g" `grep registry.tars.com -rl ./*`
  fi
}
install_master_DB

creat_db(){
  if [ "$NODETYPE" == "master" ]; then
    echo "init db config"
    cd ${TARS_INSTALL}/sql

    mysql -u${DBUser} -p${DBPassword} -h${DBIP} -P${DBPort} -e "create database db_tars"
    mysql -u${DBUser} -p${DBPassword} -h${DBIP} -P${DBPort} -e "create database tars_stat"
    mysql -u${DBUser} -p${DBPassword} -h${DBIP} -P${DBPort} -e "create database tars_property"
    mysql -u${DBUser} -p${DBPassword} -h${DBIP} -P${DBPort} -e "create database db_tars_web"
    mysql -u${DBUser} -p${DBPassword} -h${DBIP} -P${DBPort} db_tars < ${TARS_INSTALL}/sql/db_tars.sql
  fi
}
creat_db

#deploy web
if [ "$NODETYPE" == "master" ]; then
    sed -i "s/db.tars.com/$DBIP/g" `grep db.tars.com -rl ${TARS_INSTALL}/web/config/webConf.js`
    sed -i "s/tars.port.3306/$DBPort/g" `grep tars.port.3306 -rl ${TARS_INSTALL}/web/config/webConf.js`
    sed -i "s/tars.db.user/$DBUser/g" `grep tars.db.user -rl ${TARS_INSTALL}/web/config/webConf.js`
    sed -i "s/tars2015/$DBPassword/g" `grep tars2015 -rl ${TARS_INSTALL}/web/config/webConf.js`
    sed -i "s/registry.tars.com/$HOSTIP/g" `grep registry.tars.com -rl ${TARS_INSTALL}/web/config/tars.conf`

    #sed -i "s/enableAuth: false/enableAuth: true/g" ${TARS_INSTALL}/web/config/authConf.js
    #sed -i "s/enableLogin: false/enableLogin: true/g" ${TARS_INSTALL}/web/config/loginConf.js

    cd ${TARS_INSTALL}/web; pm2 stop tars-node-web; npm run prd;
fi